package guoanmaker.operator.business.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.repository.TestTokenRepository;
import guoanmaker.operator.business.service.OperatorResidueCountService;
import guoanmaker.operator.business.service.PatentQueryRecordService;
import guoanmaker.operator.business.service.PatentSearchService;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @ClassName: OperatorPatentSearchController
 * @Description: 专利相关查询
 * @author xiaoqiang
 * @date 2017年12月8日 下午4:31:22
 *
 */
@RestController
@RequestMapping(ApiController.MESSAGE_URL)
public class OperatorPatentSearchController {


	@Resource
	private PatentSearchService patentSearchService;

	@Resource
	private OperatorResidueCountService operatorResidueCountService;

	@Resource
	private PatentQueryRecordService patentQueryRecordService;

	/**
	 * 
	 * @Title: getPatentsByKeyWord 
	 * @Description: 查询指定关键字专利信息 
	 * @param keyWord
	 *         用户输入的查询条件 
	 * @param page 页码，默认值1，最大页码为50页 
	 * @param pageSize
	 *         每页展现数据的条数，默认50条，单页最大50条 
	 * @return AjaxJson @throws
	 */
	@ApiOperation(value = "查询指定关键字专利信息", notes = "查询指定关键字专利信息")
	@PostMapping(value = "getPatentsByKeyWord/{userid}")
	public AjaxJson getPatentsByKeyWord(@PathVariable(value = "userid") String userid,
			@RequestParam(value = "keyWord", required = true) String keyWord,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "pageSize", defaultValue = "50") int pageSize) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功")
					 .setData(patentSearchService.getPatentsByKeyWord(userid,keyWord,page,
					 pageSize));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}

	/**
	 * 
	 * @Title: getPatentById 
	 * @Description: 根据ID查询指定专利信息 
	 * @param id 专利ID
	 *  @return AjaxJson 
	 *  @throws
	 */
	@ApiOperation(value = "根据ID查询指定专利信息", notes = "根据ID查询指定专利信息")
	@PostMapping(value = "getPatentById/{userid}")
	public AjaxJson getPatentById(@PathVariable(value = "userid") String userid,
			@RequestParam(value = "id", required = true) String id,
			@RequestParam(value = "keyWord", required = true) String keyWord) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功")
					.setData(patentSearchService.getPatentById(userid, id, keyWord));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	/**
	 * 
	 * @Title: queryByParameter
	 * @Description: 根据条件查询专利查询记录查询
	 * @param page 页码
	 * @param pageSize 页面数据数
	 * @param title 专利名称
	 * @param phone 电话号
	 * @param username 用户名
	 * @param beginDate 开始日期
	 * @param endDate 结束日期
	 * @return AjaxJson
	 * @throws
	 */
	@ApiOperation(value = "专利查询记录查询", notes = "专利查询记录查询")
	@PostMapping(value = "queryByParameter")
	public AjaxJson queryByParameter(@RequestParam(value = "page", required = true, defaultValue = "1") int page,
			@RequestParam(value = "pageSize", required = true, defaultValue = "10") int pageSize,
			@RequestParam(value = "title", required = false) String title,
			@RequestParam(value = "phone", required = false) String phone,
			@RequestParam(value = "username", required = false) String username,
			@RequestParam(value = "beginDate", required = false) String beginDate,
			@RequestParam(value = "endDate", required = false) String endDate) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(patentQueryRecordService
					.findByParameter(page, pageSize, title, phone, username, beginDate, endDate));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @Title: queryByParameter
	 * @Description: 清空用户查询详情缓存
	 * @param userid 用户id
	 * @return AjaxJson 
	 * @throws
	 */
	@ApiOperation(value = "清空用户查询详情缓存", notes = "清空用户查询详情缓存")
	@PostMapping(value = "cleanCache/{userid}")
	public AjaxJson queryByParameter(@PathVariable(value = "userid") String userid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(patentSearchService.cleanCache(userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @Title: patentSearchDetail
	 * @Description: 剩余查询信息次数
	 * @return AjaxJson 
	 * @throws
	 */
	@ApiOperation(value = "剩余查询信息次数", notes = "剩余查询信息次数")
	@PostMapping(value = "patentSearchDetail")
	public AjaxJson patentSearchDetail(){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorResidueCountService.patentSearchDetail());
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
}
